package com.lin.cms.web.controller.assist;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lin.cms.common.web.ResponseUtils;
import com.lin.cms.criteria.ReceiverMessageCriteria;
import com.lin.cms.domain.CmsUser;
import com.lin.cms.domain.ReceiverMessage;
import com.lin.cms.domain.Site;
import com.lin.cms.service.ReceiverMessageService;
import com.lin.cms.web.CmsUtils;


@Controller
public class MessageController {
	private static final Logger log = LoggerFactory.getLogger(MessageController.class);

	// 查找未读信息条数
	@RequestMapping(value = "/message/v_countUnreadMsg.do")
	public void findUnreadMessagesByUser(HttpServletRequest request,
				HttpServletResponse response, ModelMap model) throws JSONException {
	CmsUser user = CmsUtils.getCmsUser(request);
	Site site = CmsUtils.getSite(request);
	JSONObject object = new JSONObject();
	if (user == null) {
		object.put("result", false);
	} else {                                                                              
		List<ReceiverMessage> receiverMessages = receiverMessageService.selectByExample(new ReceiverMessageCriteria(site.getSiteId(),null,user.getUserId(),null,null,null,false,0));
		object.put("result", true);
		if (receiverMessages != null && receiverMessages.size() > 0) {
			object.put("count", receiverMessages.size());
		} else {
			object.put("count", 0);
		}
		object.put("result", true);
	}
	ResponseUtils.renderJson(response, object.toString());
	}
		
	@Autowired
	private ReceiverMessageService receiverMessageService;
}